﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersonEntity
{
    public class DS_Entity : PersonProject.iDS
    {
        PersonsEntities bd = new PersonsEntities();
        public override List<PersonProject.Person> Load()
        {
            List<PersonProject.Person> pList = new List<PersonProject.Person>();
            foreach (Person p in bd.People)
            {
                pList.Add(ConvertToPerson(p));
            }
            return pList;
        }

        public override void Create(PersonProject.Person person)
        {
            bd.People.AddObject(ConvertToEntity(person));
            bd.SaveChanges();
        }

        /// <summary>
        /// Udpate method if Person not found it will call a Create method
        /// </summary>
        /// <param name="person">Person to Update/Create</param>
        public override void Update(PersonProject.Person person)
        {
            bool found = false;

            foreach (Person p in bd.People)
            {
                if (p.Id == person.Id)
                {
                    found = true;
                    Person temp = ConvertToEntity(person);
                    p.Id = temp.Id;
                    p.FName = temp.FName;
                    p.LName = temp.LName;
                    p.Age = temp.Age;
                    p.Image = temp.Image;
                    p.Memo = temp.Memo;

                    p.Adresses.Clear();
                    foreach (Adress adr in temp.Adresses)
                    {
                        Adress adress = new Adress();
                        adress.City = adr.City;
                        adress.Country = adr.Country;
                        adress.House = adr.House;
                        adress.Room = adr.Room;
                        adress.Street = adr.Street;
                        p.Adresses.Add(adress);
                    }

                    p.Contacts.Clear();
                    foreach (Contact cont in temp.Contacts)
                    {
                        Contact contact = new Contact();
                        contact.Mail = cont.Mail;
                        contact.Phone = cont.Phone;
                        contact.Skype = cont.Skype;
                        p.Contacts.Add(contact);
                    }
                    break;
                }
            }

            if (!found)
            {
                Create(person);
            }
            else
            {
                bd.SaveChanges();
            }
        }

        public override void Delete(int ID)
        {
            Person person = null;

            foreach (Person p in bd.People)
            {
                if (p.Id == ID)
                {
                    person = p;
                    break;
                }
            }

            if (person != null)
            {
                person.Contacts.Clear();
                person.Adresses.Clear();
                bd.People.DeleteObject(person);
            }
            bd.SaveChanges();
        }

        public PersonProject.Person ConvertToPerson(Person entity)
        {
            PersonProject.Person person = new PersonProject.Person();
            person.Contacts.Remove(person.Contacts[0]);
            person.Adresses.Remove(person.Adresses[0]);

            person.Age = entity.Age.Value;
            person.FName = entity.FName;
            person.LName = entity.LName;
            person.Id = entity.Id;
            person.Image = entity.Image;
            person.Memo = entity.Memo;

            foreach (Contact entCont in entity.Contacts)
            {
                PersonProject.Contact contact = new PersonProject.Contact();
                contact.Mail = entCont.Mail;
                contact.Phone = entCont.Phone;
                contact.Skype = entCont.Skype;

                person.Contacts.Add(contact);
            }

            foreach (Adress entAdr in entity.Adresses)
            {
                PersonProject.Adress adress = new PersonProject.Adress();
                adress.City = entAdr.City;
                adress.Country = entAdr.Country;
                adress.House = entAdr.House;
                adress.Room = entAdr.Room;
                adress.Street = entAdr.Street;

                person.Adresses.Add(adress);
            }
            return person;
        }

        public Person ConvertToEntity(PersonProject.Person person)
        {
            Person entity = new Person();
            entity.Age = person.Age;
            entity.FName = person.FName;
            entity.LName = person.LName;
            entity.Id = person.Id;
            entity.Image = person.Image;
            entity.Memo = person.Memo;

            foreach (PersonProject.Contact persCont in person.Contacts)
            {
                Contact contact = new Contact();
                contact.Mail = persCont.Mail;
                contact.Phone = persCont.Phone;
                contact.Skype = persCont.Skype;
                entity.Contacts.Add(contact);
            }

            foreach (PersonProject.Adress persAdr in person.Adresses)
            {
                Adress adress = new Adress();
                adress.City = persAdr.City;
                adress.Country = persAdr.Country;
                adress.House = persAdr.House;
                adress.Room = persAdr.Room;
                adress.Street = persAdr.Street;
                entity.Adresses.Add(adress);
            }

            return entity;
        }
    }
}
